var Q = require('q'),
    entityCollection = require('../../../Base/server/controllers/EntityCollection');

// 根据记录ID查找物料
exports.findById = function (itemMasterId, projection, populate) {
    var deferred = Q.defer();

    if (!projection) {
        projection = {};
    }
    var entity = entityCollection.getEntity("ItemMaster"),
        result = entity.Entity.findById(itemMasterId, projection);
    if (populate) {
        result = result.populate(populate);
    }

    result.then(function (record) {
        deferred.resolve(record);
    }, function () {
        var newErr = new Error();
        newErr.leval = 9;
        newErr.title = '查询物料错误';
        newErr.message = '查询失败，请检查查询参数！';
        deferred.reject(newErr);
    });

    return deferred.promise;
};

// 根据物料id查找物料信息
exports.findItemCategoryById = function (itemMasterId, completeFun) {
    var entity = entityCollection.getEntity("ItemMaster");
    entity.findOne({_id: itemMasterId}, null,
        function (err, record) {
            if (err) {
                var newErr = new Error();
                newErr.leval = 9;
                newErr.title = '查询物料id错误';
                newErr.message = '查询失败，请检查查询参数！';
                completeFun(newErr);
            } else {
                completeFun(null, record);
            }
        });
};